package org.foreningsgatan.modular.lightsources;

/**
 * 
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public class RGB {
	private float r;
	private float g;
	private float b;

	public RGB clone() {
		return new RGB(r, g, b);
	}

	static private float limit(float v, float min, float max) {
		if (v < min)
			return min;

		if (v > max)
			return max;

		return v;
	}

	public void multiply(float intensity) {
		r = RGB.limit(r * intensity, 0f, 1.0f);
		g = RGB.limit(g * intensity, 0f, 1.0f);
		b = RGB.limit(b * intensity, 0f, 1.0f);
	}

	public void add(RGB rgb) {
		r = RGB.limit(r + rgb.r, 0f, 1.0f);
		g = RGB.limit(g + rgb.g, 0f, 1.0f);
		b = RGB.limit(b + rgb.b, 0f, 1.0f);
	}

	public RGB(float r, float g, float b) {
		this.r = r;
		this.g = g;
		this.b = b;
	}

	public float getR() {
		return r;
	}

	public float getG() {
		return g;
	}

	public float getB() {
		return b;
	}

	public float[] getRGB() {
		return getRGB(1.0f);
	}

	public float[] getRGB(float a) {
		return new float[] { r, g, b, a };
	}
}
